package com.qianheng.kit.base.operation.orderoperation;

import com.qianheng.kit.base.dto.OrderDto;
import com.qianheng.kit.base.operation.AbstractOperationCheckerFactory;
import com.qianheng.kit.base.operation.CheckResult;
import com.qianheng.kit.base.operation.OperationChecker;

import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.function.Predicate;

public class OrderOperationCheckerFactory extends AbstractOperationCheckerFactory {

    public static OperationChecker orderBizChecker(String... bizs){
        return orderChecker(orderOperationCheckContext -> {
            if(bizs == null || bizs.length ==0){
                throw new InvalidParameterException("orderBizChecker参数biz不能为空");
            }
            OrderDto orderDto = orderOperationCheckContext.getOrderDto();
            if(orderDto == null){
                return false;
            }
           return Arrays.stream(bizs)
                    .anyMatch(biz-> biz.equals(orderDto.getBiz()));

        },"biz校验");
    }

    public static OperationChecker orderChecker(Predicate<OrderOperationCheckContext> orderCheckPredicate, String msg){
        return (OperationChecker<OrderOperationCheckContext>) checkContext -> {
            boolean test = orderCheckPredicate.test(checkContext);
            return new CheckResult(test,msg+"->"+Boolean.toString(test));
        };
    }

    /**
     * 用于测试的checker
     * @param b
     * @return
     */
    public static OperationChecker checkerTrue(boolean b) {
        return OrderOperationCheckerFactory.orderChecker(orderOperationCheckContext -> {
            return b;
        }, "永远返回" + Boolean.toString(b));
    }

}
